package cn.portx.view;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.type.StringType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.bdf3.dorado.jpa.JpaUtil;
import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;

import cn.portx.ICallback;
import cn.portx.entity.Billing;
import cn.portx.entity.OrderExpense;
import cn.portx.hibernate.HibernateDao;
import cn.portx.hibernate.ParseResult;

@Component
public class YsdzPR {

	@Autowired
	private HibernateDao hibernateDao;

	@DataProvider
	@Transactional(readOnly = true)
	public void loadOrderExpense(Page<OrderExpense> page, Criteria criteria) {
		String sql = "SELECT\n" +
				"	*\n" +
				"FROM\n" +
				"	(\n" +
				"		SELECT\n" +
				"			{oe.*}, o.order_no_,\n" +
				"			o.cnt_no_,\n" +
				"			o.bill_no_,\n" +
				"			o.voyage_,\n" +
				"			o.type_ AS order_type_,\n" +
				"			c.name_ AS customer_name_,\n" +
				"			v.name_ AS vessel_name_,\n" +
				"			ei.name_ AS expense_item_name_\n" +
				"		FROM\n" +
				"			t_order_expense oe\n" +
				"		INNER JOIN t_order o ON oe.order_id_ = o.id_\n" +
				"		LEFT JOIN t_customer c ON o.customer_id_ = c.id_\n" +
				"		LEFT JOIN t_vessel v ON o.vessel_id_ = v.id_\n" +
				"		LEFT JOIN t_expense_item ei ON oe.expense_item_id_ = ei.id_\n" +
				"		WHERE\n" +
				"			oe.type_ = '0'\n" +
				"	) t\n" +
				"WHERE\n" +
				"	1 = 1";
		String countSql = "SELECT\n" +
				"	count(*)\n" +
				"FROM\n" +
				"	(\n" +
				"		SELECT\n" +
				"			oe.*, o.order_no_,\n" +
				"			o.cnt_no_,\n" +
				"			o.bill_no_,\n" +
				"			o.voyage_,\n" +
				"			o.type_ AS order_type_,\n" +
				"			c.name_ AS customer_name_,\n" +
				"			v.name_ AS vessel_name_,\n" +
				"			ei.name_ AS expense_item_name_\n" +
				"		FROM\n" +
				"			t_order_expense oe\n" +
				"		INNER JOIN t_order o ON oe.order_id_ = o.id_\n" +
				"		LEFT JOIN t_customer c ON o.customer_id_ = c.id_\n" +
				"		LEFT JOIN t_vessel v ON o.vessel_id_ = v.id_\n" +
				"		LEFT JOIN t_expense_item ei ON oe.expense_item_id_ = ei.id_\n" +
				"		WHERE\n" +
				"			oe.type_ = '0'\n" +
				"	) t\n" +
				"WHERE\n" +
				"	1 = 1";
		Map<String, Object> parametersMap = new HashMap<String, Object>();
		
		ParseResult result = this.hibernateDao.parseCriteria(criteria, true, "t");
		if(result!=null){
			sql += " and "+result.getAssemblySql();
			countSql += " and "+result.getAssemblySql();
			parametersMap.putAll(result.getValueMap());
		}
		Session session = this.hibernateDao.getSession(OrderExpense.class);

		this.hibernateDao.pagingSQLQuery(page, sql, countSql, parametersMap, new ICallback<Void, SQLQuery>() {

			@Override
			public Void execute(SQLQuery query) {
				query.addEntity("oe", OrderExpense.class).addScalar("order_no_", StringType.INSTANCE)
				.addScalar("cnt_no_", StringType.INSTANCE).addScalar("bill_no_", StringType.INSTANCE)
				.addScalar("voyage_", StringType.INSTANCE).addScalar("order_type_", StringType.INSTANCE)
				.addScalar("customer_name_", StringType.INSTANCE).addScalar("vessel_name_", StringType.INSTANCE)
				.addScalar("expense_item_name_", StringType.INSTANCE);
				return null;
			}

		}, new ICallback<List<?>, List<?>>() {

			@Override
			public List<?> execute(List<?> list) {
				List<OrderExpense> result = new ArrayList<OrderExpense>();
				for(Object object:list){
					Object[] array = (Object[]) object;
					try {
						OrderExpense oe = EntityUtils.toEntity(array[0]);
						EntityUtils.setValue(oe, "orderNo", array[1]);
						EntityUtils.setValue(oe, "cntNo", array[2]);
						EntityUtils.setValue(oe, "billNo", array[3]);
						EntityUtils.setValue(oe, "voyage", array[4]);
						EntityUtils.setValue(oe, "orderType", array[5]);
						EntityUtils.setValue(oe, "customerName", array[6]);
						EntityUtils.setValue(oe, "vesselName", array[7]);
						EntityUtils.setValue(oe, "expenseItemName", array[8]);
						result.add(oe);
					} catch (Exception e) {
						throw new RuntimeException(e);
					}
				}
				return result;
			}
		}, session);
	}

	@DataProvider
	@Transactional(readOnly = true)
	public List<Billing> loadExpenseBilling(String orderExpenseId) {
		return JpaUtil.linq(Billing.class).equal("orderExpenseId", orderExpenseId).list();
	}

	@DataResolver
	@Transactional
	public void hexiao(List<OrderExpense> list) {

	}
}
